<template>
  <div>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-row>
        <el-form-item>
          <el-button type="success" :disabled="processId===null" @click="showAdd()">新增</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      style="width: 100%;margin-bottom: 20px;"
      border
      :header-cell-style="{textAlign : 'center'}"
      :cell-style="{textAlign : 'center'}"
    >
      <el-table-column
        prop="processNodeCode"
        label="流程节点编码"
      >
      </el-table-column>
      <el-table-column
        prop="processNodeName"
        label="流程节点名"
      >
      </el-table-column>
      <el-table-column
        label="审批类型"
      >
        <template slot-scope="{row}">
          <el-tag v-if="row.approvalType === 'one_sign'">单人审批</el-tag>
          <el-tag v-if="row.approvalType === 'or_sign'" type="success">多人或签</el-tag>
          <el-tag v-if="row.approvalType === 'and_sign'" type="warning">多人会签</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button type="text" size="small" @click="showBindingApprover(row.id)">审批人绑定</el-button>
          <el-button type="text" size="small" @click="showView(row.id)">查看</el-button>
          <el-button type="text" size="small" @click="showUpdate(row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="ensureDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="total,prev, pager, next"
      :current-page.sync="page"
      :page-size="pageSize"
      :total="total"
      @current-change="getPage"
    >
    </el-pagination>
    <opt-process-node :dialog-visible.sync="optDialog" :opt-flag="optFlag" :process-node-id="processNodeId"
                      :process-id="processId"
                      @refresh="getPage"
    />
    <binding-approver :dialog-visible.sync="bingingApproverDialog" :process-id="processId"
                      :process-node-id="processNodeId"
    />
  </div>
</template>

<script>
import processNodeApi from '@/api/common/processNodeApi'
import OptProcessNode from '@/views/common/process/components/OptProcessNode.vue'
import BindingApprover from '@/views/common/process/components/BindingApprover.vue'

export default {
  name: 'ProcessNodeList',
  components: { BindingApprover, OptProcessNode },
  props: {
    processId: Number
  },
  data() {
    return {
      query: {
        processId: null
      },
      page: 1,
      pageSize: 10,
      queryLoading: false,
      dataList: [],
      total: 0,
      optDialog: false,
      optFlag: '',
      processNodeId: null,
      bingingApproverDialog: false
    }
  },
  watch: {
    processId() {
      if (this.processId) {
        this.query.processId = this.processId
        this.getPage()
      } else {
        this.query = {
          processId: null
        }
        this.dataList = []
        this.total = 0
      }
    }
  },
  methods: {
    getPage(pageNum = 1) {
      this.queryLoading = true
      this.page = pageNum
      processNodeApi.getPage(this.page, this.pageSize, this.query).then(res => {
        this.dataList = res.data.list
        this.total = res.data.total
      }).finally(() => {
        this.queryLoading = false
      })
    },
    showView(processNodeId) {
      this.optDialog = true
      this.optFlag = 'view'
      this.processNodeId = processNodeId
    },
    showAdd() {
      this.optDialog = true
      this.optFlag = 'add'
      this.processNodeId = null
    },
    showUpdate(processNodeId) {
      this.optDialog = true
      this.optFlag = 'update'
      this.processNodeId = processNodeId
    },
    ensureDelete(processNodeId) {
      this.$confirm('确定执行删除操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteOpt(processNodeId)
      }).catch(() => {
      })
    },
    deleteOpt(processNodeId) {
      processNodeApi.delete(processNodeId).then(res => {
        this.$message({
          message: '操作成功！',
          type: 'success'
        })
        this.getPage()
      })
    },
    showBindingApprover(processNodeId) {
      this.bingingApproverDialog = true
      this.processNodeId = processNodeId
    },
    reset() {
      this.query = {}
    }
  }
}
</script>

<style scoped>
.el-pagination {
  text-align: right;
}
</style>
